package com.bipbip.repositorio;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.bipbip.modelo.Viaje;

@Repository(value = "viajeDAO")
@Transactional(readOnly = true)
public class JPAViajeDAOImpl implements ViajeDAO {

	private EntityManager em = null;

	/*
	 * Sets the entity manager.
	 */
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public Viaje insert(Viaje viaje) {
		em.persist(viaje);
		return viaje;
	}

	public List<Viaje> selectAll() {
		return em.createQuery("select v from Viaje v").getResultList();
	}

	public Viaje select(int id) {
		return em.find(Viaje.class, id);
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public void delete(Viaje v) {
		em.remove(em.merge(v));
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public Viaje update(Viaje v) {
		return em.merge(v);
	}

}
